package com.leave.mos.service.impl;

import com.leave.mos.constant.TablenameConstant;
import com.leave.mos.entity.Order;
import com.leave.mos.entity.User;
import com.leave.mos.service.find.MongodbSelectService;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.core.query.UpdateDefinition;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 查询方法的使用
 */
@Service
public class MongodbSelectServiceImpl implements MongodbSelectService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public List<User> findClass(User user) {

        Query query = new Query();
        query.with(Sort.by(Sort.Direction.DESC, "time_stamp"));
        //query.limit(1); 只查询一条数据
        List<User> users = mongoTemplate.find(query, User.class);
        return users;
    }

    @Override
    public List<User> findTable(User user) {

        Query query = new Query();
        List<User> users = mongoTemplate.find(query, User.class, TablenameConstant.Testabc.TableName);
        return users;
    }

    @Override
    public List<Order> findAll() {

        List<Order> orders = mongoTemplate.findAll(Order.class);
        return orders;
    }

    @Override
    public List<Order> findAllTable() {

        List<Order> orders = mongoTemplate.findAll(Order.class, TablenameConstant.Order.TableName);
        return orders;
    }

    /**
     * 查询到的数据会在 集合中删除掉
     * @return
     */
    @Override
    public List<Order> findAllAndRemove() {
        Query query = new Query();
        query.limit(5);
        List<Order> orders = mongoTemplate.findAllAndRemove(query, TablenameConstant.Order.TableName);
        return orders;
    }


    //查询数据并修改
    @Override
    public Order findAndModify() {
        Query query = new Query();
        query.limit(5);

        UpdateDefinition updateDefinition = new Update();
        Order orders = mongoTemplate.findAndModify(query,updateDefinition, Order.class,TablenameConstant.Order.TableName);
        return orders;
    }

    @Override
    public List<Order> findDistinct() {
        Query query = new Query();
        query.limit(5);
        List<Order> orders = mongoTemplate.findDistinct(query,TablenameConstant.Order.dealName,TablenameConstant.Order.TableName,Order.class);
        return orders;
    }

    @Override
    public Order findAndRemove() {
        Query query = new Query();
        query.limit(5);
        Order orders = mongoTemplate.findAndRemove(query,Order.class, TablenameConstant.Order.TableName);
        return orders;
    }


    @Override
    public String findAndReplace() {
        Query query = new Query();
        query.limit(5);
        String id = "id";
        String orders = mongoTemplate.findAndReplace(query,id, TablenameConstant.Order.TableName);
        return orders;
    }

    @Override
    public Order findOne() {
        Query query = new Query();
        query.limit(5);
        Order orders = mongoTemplate.findOne(query,Order.class,TablenameConstant.Order.TableName);
        return orders;
    }

    @Override
    public Order findById(String id) {
        Order orders = mongoTemplate.findById(id, Order.class);
        return orders;
    }

}
